package com.tdam2011.grupo06.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Date;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.xmlpull.v1.XmlSerializer;

import android.text.format.DateFormat;
import android.util.Xml;

public class Networking {
	
	private static final String SERVER_URL = "https://messagesender-miruli.rhcloud.com/ms/";
	private static int requestValue = (int)(Math.random()*1000);
	
	/**
	 * Sends a user registration request to the server. Use a POST request.
	 * @param user Username
	 * @param password User's Passwords. Must have 6 characters minimun.
	 * @return The XML response
	 */
	public static InputStream requestUserRegistration(String user, String password)
	{
		XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	    	serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("", "action");
	        serializer.attribute("", "id", Integer.toString(++requestValue));
	        serializer.attribute("", "name", "register-user");
	        serializer.startTag("", "action-detail");
	        serializer.startTag("", "user");
	        serializer.attribute("", "username", user);
	        serializer.attribute("", "password", password);
	        serializer.endTag("", "user");
	        serializer.endTag("", "action-detail");
	        serializer.endTag("", "action");
	        serializer.endDocument();

	        String xml = writer.toString();

			//make the Post Request
			HttpEntity entity = postRequest(xml);
			return entity.getContent();
		} 
		catch (ClientProtocolException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} 
		catch (IOException e) 
		{
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}	
		
	}
	
	public static InputStream requestSendMessage(String user, String password, String recipient, String message)
	{
		XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	    	serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("", "action");
	        serializer.attribute("", "id", Integer.toString(++requestValue));
	        serializer.attribute("", "name", "send-message");
	        serializer.startTag("", "action-detail");
	        serializer.startTag("", "auth");
	        serializer.attribute("", "username", user);
	        serializer.attribute("", "key", password);
	        serializer.endTag("", "auth");
	        serializer.startTag("", "message");
	        serializer.attribute("", "to", recipient);
	        serializer.cdsect(message);
	        serializer.endTag("", "message");
	        serializer.endTag("", "action-detail");
	        serializer.endTag("", "action");
	        serializer.endDocument();

	        String xml = writer.toString();
		
			//make the Post Request
			HttpEntity hr = postRequest(xml);

			return hr.getContent();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		}				
	}
	
	public static InputStream requestGetMessages(String user, String password)
	{
		return requestGetMessages(user, password, new Date());
		
	}
	
	public static InputStream requestGetMessages(String user, String password, Date timeFilter)
	{
		XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	        serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("", "action");
	        serializer.attribute("", "id", Integer.toString(++requestValue));
	        serializer.attribute("", "name", "get-messages");
	        serializer.startTag("", "action-detail");
	        serializer.startTag("", "auth");
	        serializer.attribute("", "username", user);
	        serializer.attribute("", "key", password);
	        serializer.endTag("", "auth");
	        serializer.startTag("", "filter");
	        serializer.attribute("", "type", "timestamp");
	        serializer.text(DateFormat.format("dd/MM/yyyy kk:mm:ss",timeFilter).toString());
	        serializer.endTag("", "filter");
	        serializer.endTag("", "action-detail");
	        serializer.endTag("", "action");
	        serializer.endDocument();
	        
	        String xml = writer.toString();

			//make the Post Request
			HttpEntity hr = postRequest(xml);

			return hr.getContent();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new RuntimeException(e);
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}		
	}
	
	public static HttpEntity postRequest (String xml) throws ClientProtocolException, IOException
	{
		HttpPost httppost = new HttpPost(SERVER_URL);  
		StringEntity se = new StringEntity(xml);
		
		se.setContentType("text/xml");  
		
		httppost.setEntity(se);  

		HttpClient httpclient = new DefaultHttpClient();
		BasicHttpResponse httpResponse = 
		    (BasicHttpResponse) httpclient.execute(httppost);
		return httpResponse.getEntity();

	}

	public static HttpEntity getRequest() throws ClientProtocolException, IOException
	{
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(SERVER_URL);
		HttpResponse response = httpClient.execute(httpGet);
		return response.getEntity();
	}
	
}
